package xyz.riceball.develop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.riceball.develop.entity.po.AssistSearchPO;
import xyz.riceball.develop.entity.po.DictionaryPO;
import xyz.riceball.develop.mapper.AssistSearchMapper;
import xyz.riceball.develop.service.AssistSearchService;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.ApiRc;
import xyz.riceball.framework.core.context.UserContext;
import xyz.riceball.framework.core.exception.RiceBallBizWarnException;
import xyz.riceball.framework.core.util.BeanUtils;
import xyz.riceball.framework.core.util.MessageUtils;

import java.util.List;

/**
 * 辅助缓存 Service实现层
 *
 * @author xiaovcloud
 * @since 2022-07-02 10:58:37
 */
@Service("assistSearchService")
public class AssistSearchServiceImpl extends ServiceImpl<AssistSearchMapper, AssistSearchPO> implements AssistSearchService {

    @Override
    public Page<AssistSearchPO> selectPage(Page<AssistSearchPO> page, AssistSearchPO assistSearchPO) {
        LambdaQueryWrapper<AssistSearchPO> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(assistSearchPO.getCode())) {
            wrapper.eq(AssistSearchPO::getCode, assistSearchPO.getCode());
        }
        if (StrUtil.isNotEmpty(assistSearchPO.getCode())) {
            wrapper.like(AssistSearchPO::getName, assistSearchPO.getName());
        }
        return page(page, wrapper);
    }

    @Override
    public ApiR<AssistSearchPO> add(AssistSearchPO assistSearchPO) {
        checkAssistSearchBeforeAdd(assistSearchPO);
        BeanUtils.buildAutoField(assistSearchPO, true, UserContext.getUserId());
        int insert = this.getBaseMapper().insert(assistSearchPO);
        return ApiR.msgData(insert == 1, "新增菜单成功", "新增菜单失败", assistSearchPO);
    }

    /**
     * 新增前的检测
     *
     * @param assistSearchPO
     */
    private void checkAssistSearchBeforeAdd(AssistSearchPO assistSearchPO) {
        List<AssistSearchPO> assistSearchPOList;
        // 新增
        LambdaQueryWrapper<AssistSearchPO> wrapperCode = Wrappers.<AssistSearchPO>lambdaQuery();
        wrapperCode.eq(AssistSearchPO::getCode, assistSearchPO.getCode());
        assistSearchPOList = this.getBaseMapper().selectList(wrapperCode);
        //新增，判断name是否重复
        if (assistSearchPOList.size() > 0) {
            throw new RiceBallBizWarnException("辅助查询编码重复：{}", assistSearchPO.getName());
        }

        LambdaQueryWrapper<AssistSearchPO> wrapperName = Wrappers.<AssistSearchPO>lambdaQuery();
        wrapperName.eq(AssistSearchPO::getName, assistSearchPO.getName());
        assistSearchPOList = this.getBaseMapper().selectList(wrapperName);
        //新增，判断name是否重复
        if (assistSearchPOList.size() > 0) {
            throw new RiceBallBizWarnException("辅助查询名称重复：{}", assistSearchPO.getName());
        }
    }


    @Override
    public boolean edit(AssistSearchPO assistSearchPO) {
        checkAssistSearchBeforeEdit(assistSearchPO);
        AssistSearchPO assistSearch = this.getBaseMapper().selectById(assistSearchPO.getId());
        BeanUtils.copyWithoutNull(assistSearch, assistSearchPO);
        BeanUtils.buildAutoField(assistSearch, false, UserContext.getUserId());
        int edit = this.getBaseMapper().updateById(assistSearch);
        return edit == 1;
    }

    /**
     * 修改前的检测
     *
     * @param assistSearchPO
     */
    private void checkAssistSearchBeforeEdit(AssistSearchPO assistSearchPO) {
        List<AssistSearchPO> menuList;
        // 修改
        AssistSearchPO menu = getBaseMapper().selectById(assistSearchPO.getId());
        if (menu == null) {
            throw new RiceBallBizWarnException("通过ID：{}未能查询到菜单", assistSearchPO.getId());
        }

        LambdaQueryWrapper<AssistSearchPO> wrapperCode = Wrappers.<AssistSearchPO>lambdaQuery();
        wrapperCode.eq(AssistSearchPO::getCode, assistSearchPO.getCode());
        menuList = this.getBaseMapper().selectList(wrapperCode);
        //修改，判断名字，除了这个id的，其他的算重复
        if (menuList.size() > 1) {
            throw new RiceBallBizWarnException("辅助查询编码重复：{}", assistSearchPO.getName());
        }
        if (menuList.size() == 1) {
            if (!menuList.get(0).getId().equals(assistSearchPO.getId())) {
                throw new RiceBallBizWarnException("辅助查询编码重复：{}", assistSearchPO.getName());
            }
        }

        LambdaQueryWrapper<AssistSearchPO> wrapperName = Wrappers.<AssistSearchPO>lambdaQuery();
        wrapperName.eq(AssistSearchPO::getName, assistSearchPO.getName());
        menuList = this.getBaseMapper().selectList(wrapperName);
        //修改，判断名字，除了这个id的，其他的算重复
        if (menuList.size() > 1) {
            throw new RiceBallBizWarnException("辅助查询名称重复：{}", assistSearchPO.getName());
        }
        if (menuList.size() == 1) {
            if (!menuList.get(0).getId().equals(assistSearchPO.getId())) {
                throw new RiceBallBizWarnException("辅助查询名称重复：{}", assistSearchPO.getName());
            }
        }
    }

    @Override
    public ApiRc<Void> delete(List<String> ids) {
        ApiRc<Void> delete = ApiRc.buildResultCount(ids.size());
        for (String id : ids) {
            try {
                int i = getBaseMapper().deleteById(id);
                if (i == 0) {
                    delete.append(MessageUtils.format("id为[{}]的菜单不存在", id));
                    continue;
                }
                delete.success();
            } catch (Exception e) {
                log.error("删除字典分类失败！", e);
                delete.append(MessageUtils.format("id为{}辅助查询删除失败:{}<br/>", id, e.getMessage()));
            }
        }
        return ApiRc.getResult(delete);
    }
}